package com.platform.upms.controller;

import com.platform.common.config.Global;
import com.platform.common.page.Page;
import com.platform.common.utils.UploadImageToString;
import com.platform.common.utils.UserUtils;
import com.platform.upms.model.UpmsPicture;
import com.platform.upms.model.UpmsUser;
import com.platform.upms.model.json.Tip;
import com.platform.upms.service.UpmsPictureService;
import com.platform.upms.service.UpmsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping("/picture")
public class UpmsPictureController {

    @Autowired
    private UpmsPictureService upmsPictureService;

    @Autowired
    private UpmsUserService upmsUserService;

    @RequestMapping("/list")
    public  String list(Model model, Page<UpmsPicture> page, HttpSession session, UpmsPicture picture){
        picture.setUserId(UserUtils.getUserId());
        page.setPageSize(12);
        Page<UpmsPicture> p = upmsPictureService.findByPage(page,picture);
        model.addAttribute("page", p);
        return "/picture/list.jsp";
    }

    @RequestMapping("/add")
    public String add(){
        return  "/picture/add.jsp";
    }


    @RequestMapping("/insert")
    @ResponseBody
    public Tip insert(HttpServletRequest request, HttpSession session, String name, Integer size, String src) {
        Tip tip = new Tip();
        Double kbSize=Double.valueOf(size)/1000;
        Integer sessionUserId = UserUtils.getUserId();
        String urlStr = UploadImageToString.pictureToString(src, request, sessionUserId, name); //图片的路径
        UpmsPicture upmsPicture=new UpmsPicture(sessionUserId,new Date(),urlStr,"1",Double.valueOf(kbSize),name);
        Tip tip1 = updateVolume(kbSize, session);
        if(tip1.getCode() !=null && tip1.getCode()==1){
            tip.setCode(2);
            tip.setMsg("可用空间不足，保存失败");
            tip.setSuccess(false);
            return tip;
        }
        try{
            upmsPictureService.saveData(upmsPicture);
        }catch (Exception e){
            e.printStackTrace();
            tip = new Tip(1,"上传失败");
        }
        return tip;
    }

    /**
     * 上传图片时修改该用户的可用空间大小
     */
    public Tip updateVolume(Double picSize,HttpSession session){
        UpmsUser upmsUser = (UpmsUser) session.getAttribute(Global.UPMSUSER);
        Integer sessionUserId = upmsUser.getId();
        UpmsUser byId = upmsUserService.findById(sessionUserId);
        Double availableSpace = byId.getAvailableSpace();//获取到可用的空间
        Double availKB=availableSpace*1024;
        if(availKB>=picSize){
            Double  compute=availKB-picSize;
            upmsUser.setAvailableSpace(compute/1024);
            upmsUserService.update(upmsUser);
            return new Tip();
        }else{
            return  new Tip(1,"可用空间不足");
        }
    }

    /**
     * 图片删除，用户的可用空间增加
     * @param imgSize
     * @param session
     */
    public  void deletePicture(double imgSize,HttpSession session){
        UpmsUser upmsUser = (UpmsUser) session.getAttribute(Global.UPMSUSER);
        Integer sessionUserId = upmsUser.getId();
        UpmsUser byId = upmsUserService.findById(sessionUserId);
        Double availableSpace = byId.getAvailableSpace();//获取到可用的空间
        Double availKB=availableSpace*1024;//可用空间转化为
        Double  compute=availKB+imgSize;
       upmsUser.setAvailableSpace(compute/1024);
       upmsUserService.update(upmsUser);
    }

    /**
     * 单独删除一条数据
     * @param picId
     * @return
     */
    @RequestMapping("/deleteById")
    @ResponseBody
    public Tip deleteById(String picId,HttpSession session){
        try {
            UpmsPicture upmsPicture = upmsPictureService.get(picId);
            upmsPictureService.deleteDataById(upmsPicture);
            deletePicture(upmsPicture.getImgSize(),session);
            return  new Tip();
        }catch (Exception e){
            e.printStackTrace();
            return new Tip(1,"删除失败");
        }
    }

    /**
     * 删除多条数据
     * @param ids
     * @return
     */
    @RequestMapping("/deleteByIds")
    @ResponseBody
    public Tip deleteByIds(String ids[]){
        try {
            upmsPictureService.deleteDataByIds(ids);
            return  new Tip();
        }catch (Exception e){
            e.printStackTrace();
            return new Tip(1,"删除失败");
        }
    }

    /**
     * 选择图片
     * @return
     */
    @RequestMapping("/chooseImg")
    public String chooseImg(HttpSession session, ModelMap modelMap, Page page){
        UpmsUser upmsUser= (UpmsUser) session.getAttribute(Global.UPMSUSER);
        Integer id = upmsUser.getId();//当前用户的ID
        UpmsPicture picture=new UpmsPicture();
        picture.setUserId(id);
        Page<UpmsPicture> pictureList =upmsPictureService .findByPage(page,picture);
        modelMap.addAttribute("pictureList",pictureList);
        return  "/picture/imgList.jsp";
    }
}
